// pages/mimaxiugai/mimaxiugai.js

const updatePwdUrl = require('../../utils/config').updatePwdUrl;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weixinOpenId: '',

  },

  mima_tijiao: function (e) {
    var that = this;
    var oldPassword = e.detail.value.oldPassword;
    var newPassword = e.detail.value.newPassword;
    var conPassword = e.detail.value.conPassword;

    if (oldPassword == "") {
      wx.showModal({
        title: '提示',
        content: '请输入原密码',
        showCancel: false,
        confirmColor: '#30a1db'
      });
      return;
    }
    if (newPassword == "") {
      wx.showModal({
        title: '提示',
        content: '请输入新密码',
        showCancel: false,
        confirmColor: '#30a1db',
      });
      return;
    }
    if (conPassword == "") {
      wx.showModal({
        title: '提示',
        content: '请输入确认密码',
        showCancel: false,
        confirmColor: '#30a1db',

      });
      return;
    }
    if (newPassword != conPassword) {
      wx.showModal({
        title: '提示',
        content: '新密码与确认密码不一致。',
        showCancel: false,
        confirmColor: '#30a1db',
      });
      return;
    }
    wx.request({
      url: updatePwdUrl,
      method: 'POST',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token'),
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        'oldPassword': oldPassword,
        'newPassword': newPassword,
      },
      success: function (result) {
        console.log(result);
        if (result.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: result.data.msg,
            showCancel: false,
            confirmColor: '#30a1db',
            success: function () {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          });

        } else {
          wx.showModal({
            title: '提示',
            content: result.data.msg,
            showCancel: false,
            confirmColor: '#30a1db',
          });
         
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "密码修改",
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 判断是否授权
    if (wx.getStorageSync('openId') != '') {
      if (wx.getStorageSync('token') == '' || wx.getStorageSync('token') == null) {
        wx.showToast({
          title: '你已授过权, 请登录',
        })
        wx.redirectTo({
          url: '../../pages/login/login'
        })
      } else {

      }
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未授权登录，是否授权登录',
        cancelText: '取消',
        cancelColor: 'red',
        confirmText: '确认',
        confirmColor: '#30a1db',
        success: function (res) {
          //点击取消
          if (res.cancel) {
            wx.switchTab({
              url: '../../pages/index/index',
            })
          } else {
            //点击确定
            wx.redirectTo({
              url: '../../pages/userlogin/login'
            })
          }

        },
        fail: function () { }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})